#include <Editor/StdAfx.h>
#include "TranslationHelper.h"

#include <wx/dir.h>
#include <wx/config.h>
#include <wx/filename.h>

#define CATALOG_NAME wxT("GameTool")

TranslationHelper::TranslationHelper(wxApp & app)
	: m_App(app), m_Locale(NULL)
{
	Load();
}

TranslationHelper::~TranslationHelper()
{
	Save();
	if(m_Locale)
	{
		wxDELETE(m_Locale);
	}
}

wxLocale * TranslationHelper::GetLocale()
{
	return m_Locale;
}

bool TranslationHelper::Load()
{
	wxConfig config(m_App.GetAppName());
	long language;
	if(!config.Read(wxT("wxTranslation_Language"), &language, wxLANGUAGE_UNKNOWN))
	{
		language = wxLANGUAGE_DEFAULT;
	}
	
	wxArrayString names;
	wxArrayLong identifiers;
	GetInstalledLanguages(names, identifiers);
	for(size_t i = 0; i < identifiers.Count(); i++)
	{
		if(identifiers[i] == language)
		{
			if(m_Locale)
				wxDELETE(m_Locale);
			m_Locale = new wxLocale;
			m_Locale->Init(identifiers[i]);
			wxString dir = wxPathOnly(m_App.argv[0]) + wxFileName::GetPathSeparator() + wxT("lang");
			m_Locale->AddCatalogLookupPathPrefix( dir );
			m_Locale->AddCatalog(CATALOG_NAME);
			return true;
		}
	}
	return false;
}

void TranslationHelper::Save(bool bReset)
{
	wxConfig config(m_App.GetAppName());
	long language = wxLANGUAGE_DEFAULT;
	if(!bReset)
	{
		if(m_Locale)
		{
			language = m_Locale->GetLanguage();
		}
	}
	config.Write(wxT("wxTranslation_Language"), language);
	config.Flush();
}

void TranslationHelper::GetInstalledLanguages(wxArrayString & names, wxArrayLong & identifiers)
{
	names.Clear();
	identifiers.Clear();
	wxString strLangDir = wxPathOnly(m_App.argv[0]) + wxFileName::GetPathSeparator() + wxT("lang");
	wxDir dir(strLangDir);
	wxString strDirectory;
	const wxLanguageInfo* langinfo;	
	names.Add(_("English"));
	identifiers.Add(wxLANGUAGE_ENGLISH);
	for(bool cont = dir.GetFirst(&strDirectory, wxT("*.*"), wxDIR_DIRS);
		cont; cont = dir.GetNext(&strDirectory))
	{
		wxLogTrace( wxTraceMask(), _("wxTranslationHelper: file found = \"%s\""),
			strDirectory.GetData() );
		if ( wxFileExists(strLangDir + wxFileName::GetPathSeparator() + 
			strDirectory + wxFileName::GetPathSeparator() + CATALOG_NAME + wxT(".mo")) )
		{
			langinfo = wxLocale::FindLanguageInfo(strDirectory);
			if(langinfo != NULL)
			{
				names.Add(langinfo->Description);
				identifiers.Add(langinfo->Language);
			}
		}
	}
}

bool TranslationHelper::AskUserForLanguage(wxArrayString & names, wxArrayLong & identifiers)
{
	wxCHECK_MSG( names.Count() == identifiers.Count(), false,
		_("Array of language names and identifiers should have the same size.") );
	long index = wxGetSingleChoiceIndex( wxT("Select your language"), _("Language"), names);
	if(index != -1)
	{
		if(m_Locale)
		{
			wxDELETE(m_Locale);
		}
		m_Locale = new wxLocale;
		if (!wxLocale::IsAvailable(wxLANGUAGE_DEFAULT))
			fastbird::IEngine::Log(FB_DEFAULT_DEBUG_ARG, "Default locale is not avaliable.");
		if (!m_Locale->Init(identifiers[index], wxLOCALE_LOAD_DEFAULT))
			fastbird::IEngine::Log(FB_DEFAULT_DEBUG_ARG, "Init locale failed.");
		wxString dir = wxPathOnly(m_App.argv[0]) + wxFileName::GetPathSeparator() + wxT("lang");
		m_Locale->AddCatalogLookupPathPrefix( dir );
		wxLogTrace( wxTraceMask(), _("wxTranslationHelper: Path Prefix = \"%s\""),	dir );
		if(!m_Locale->AddCatalog(CATALOG_NAME))
			fastbird::IEngine::Log(FB_DEFAULT_DEBUG_ARG, "Adding catalog failed.");
		return true;
	}
	return false;
}